<?php
namespace app\wempapi\controller;
use app\common\controller\Frontend;
use app\weapp\library\MpAppApi;
use app\wempapi\model\MpWework;
use think\facade\Session;

class Mpwechat extends Frontend
{
    protected $noNeedLogin = ['sessioncode'];

    public function sessioncode(){
        if($this->request->isPost()){
            $code = $this->request->post('code');
            $mpAppApi = new MpAppApi();
            $res = $mpAppApi->sessioncode($code);
            if($res === false){
                $this->error($mpAppApi->getMessage());
            }else{
                $mpwework = MpWework::where('openid',$res)->find();
                if(empty($mpwework)){
                    $mpwework = MpWework::create(['openid'=>$res]);
                }
                Session::set('mpwework',$mpwework->toArray());
                $this->success('success',Session::get('mpwework'));
            }
        }
    }
    
}